package com.china08.yxyapi.api.home.index.news;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.platform.YxyHomeNews;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.YxyHomeNewsRepository;
import com.china08.yxyapi.util.DateToolUtils;

import io.swagger.annotations.ApiOperation;

/**
 * 首页--新闻详情查询
 * 
 */
@CrossOrigin
@RestController
public class IndexListNewsController extends YxyApiBaseController {

    @Autowired
    private YxyHomeNewsRepository yxyHomeNewsRepository;

    @ApiOperation(value = "首页咨询详情")
    @ResponseBody
    @RequestMapping(value = "/api_yw/home/index/news/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Object precess(//
            HttpServletRequest request, //
            HttpServletResponse response, //
            @RequestBody(required = false) RepModel reqModel) {

        String schoolId = reqModel.schoolId;
        Pageable pageable = reqModel.getPageable(Direction.DESC, "createdDate");
        Page<YxyHomeNews> page = yxyHomeNewsRepository.findByEdbIdAndStatus(schoolId, 1, pageable);
        List<RespModel> list = new ArrayList<IndexListNewsController.RespModel>();
        for (YxyHomeNews yxyHomeNews : page) {
            RespModel model = new RespModel();
            model.id = yxyHomeNews.getId();
            model.title = yxyHomeNews.getTitle();
            model.createdDate = DateToolUtils.format(yxyHomeNews.getCreatedDate(), DateToolUtils.FORMAT_SHORT_YMD);
            model.cover = yxyHomeNews.getCover();
            // 内容
            String content = Jsoup.parse(yxyHomeNews.getContent()).text().replace("&nbsp;", "");
            if (content.length() > 100) {
                content = StringUtils.substring(content, 0, 100) + "...";
            }
            model.content = content;
            list.add(model);
        }
        Page<RespModel> respModel = new PageImpl<IndexListNewsController.RespModel>(list, pageable,
                page.getTotalElements());
        return respModel;
    }

    protected static class RepModel extends YxyApiPageReqModel {

        // 域名
        public String domain;

        public String schoolId;

        @Override
        public void autoCheck() {
            Assert.notNull(StringUtils.defaultIfBlank(domain, null), "域名找不到了，请刷新页面");
        }
    }

    protected static class RespModel extends YxyApiRespModel {
        // 新闻id
        public String id;
        // 封面
        public String cover;
        // 标题
        public String title;
        // 内容
        public String content;
        // 创建时间
        public String createdDate;
    }
}
